// 获取合并后的 apijson 接口返回数据。
// 多表的相同字段名，用 prefix 方式解决，prefix结构：{表1: 前缀1, 表2: 前缀2}
export const getMergeDataList = (data, listName = 'dataList', prefix = {}) => {
  if (isNilOrEmpty(data) || isNilOrEmpty(data[listName]) || typeof data[listName] !== 'object') return []
  return data[listName].map(e => {
    let item = {}
    Object.keys(e).forEach(v => {
      Object.keys(e[v]).forEach(n => {
        if (isNotNilOrEmpty(prefix[v])) {
          const colName = `${prefix[v]}_${n}`
          item[colName] = e[v][n]
        } else {
          item[n] = e[v][n]
        }
      })
    })
    return item
  })
}
// 规范化 apijson 的返回数据，使之和接口标准一致
export const parseRespData = (response, listName = 'dataList') => {
  const { data, status } = response
  if (status !== 200 || !data.ok) {
    return { status, error: '数据获取失败！', data }
  }
  if (data.hasOwnProperty(listName)) {
    const dataList = getMergeDataList(data, listName)
    return {
      status,
      data: {
        ...data,
        dataList,
        total: data.info.total,
        limit: data.info.count,
        offset: data.info.count * data.info.page,
      },
    }
  } else if (data.hasOwnProperty('info')) {
    return {
      status,
      data: {
        ...data,
        total: data.info.total,
        limit: data.info.count,
        offset: data.info.count * data.info.page,
      },
    }
  } else {
    return { data, status, error: '数据获取失败！' }
  }
}
